package com.ruoyi.abuwx.service.impl;

import com.ruoyi.abuwx.domain.AbuwxQuestionChoice;
import com.ruoyi.abuwx.mapper.AbuwxQuestionChoiceMapper;
import com.ruoyi.abuwx.service.IAbuwxQuestionChoiceService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;



/**
 * 题库单选题Service业务层处理
 * 
 * @author tang
 * @date 2022-07-02
 */
@Service
public class AbuwxQuestionChoiceServiceImpl implements IAbuwxQuestionChoiceService 
{
    private static final Logger log = LoggerFactory.getLogger(AbuwxQuestionChoiceServiceImpl.class);
    @Autowired
    private AbuwxQuestionChoiceMapper abuwxQuestionChoiceMapper;

    @Autowired
    private ISysConfigService configService;


    @Autowired
    protected Validator validator;


    /**
     * 查询题库单选题
     * 
     * @param id 题库单选题主键
     * @return 题库单选题
     */
    @Override
    public AbuwxQuestionChoice selectAbuwxQuestionChoiceById(Long id)
    {
        return abuwxQuestionChoiceMapper.selectAbuwxQuestionChoiceByIds(id);
    }
    /**
     * 查询题库单选题
     *
     * @param id 题库单选题主键
     * @return 题库单选题
     */
    @Override
    public AbuwxQuestionChoice selectAbuwxQuestionChoiceByIds(Long id)
    {
        return abuwxQuestionChoiceMapper.selectAbuwxQuestionChoiceByIds(id);
    }

    /**
     * 查询题库单选题列表
     * 
     * @param abuwxQuestionChoice 题库单选题
     * @return 题库单选题
     */
    @Override
    public List<AbuwxQuestionChoice> selectAbuwxQuestionChoiceList(AbuwxQuestionChoice abuwxQuestionChoice)
    {
        return abuwxQuestionChoiceMapper.selectAbuwxQuestionChoiceList(abuwxQuestionChoice);
    }

    /**
     * 新增题库单选题
     * 
     * @param abuwxQuestionChoice 题库单选题
     * @return 结果
     */
    @Override
    public int insertAbuwxQuestionChoice(AbuwxQuestionChoice abuwxQuestionChoice)
    {
        abuwxQuestionChoice.setCreateTime(DateUtils.getNowDate());
        return abuwxQuestionChoiceMapper.insertAbuwxQuestionChoice(abuwxQuestionChoice);
    }

    /**
     * 修改题库单选题
     * 
     * @param abuwxQuestionChoice 题库单选题
     * @return 结果
     */
    @Override
    public int updateAbuwxQuestionChoice(AbuwxQuestionChoice abuwxQuestionChoice)
    {
        abuwxQuestionChoice.setUpdateTime(DateUtils.getNowDate());
        return abuwxQuestionChoiceMapper.updateAbuwxQuestionChoice(abuwxQuestionChoice);
    }

    /**
     * 批量删除题库单选题
     * 
     * @param ids 需要删除的题库单选题主键
     * @return 结果
     */
    @Override
    public int deleteAbuwxQuestionChoiceByIds(Long[] ids)
    {
        return abuwxQuestionChoiceMapper.deleteAbuwxQuestionChoiceByIds(ids);
    }

    /**
     * 删除题库单选题信息
     * 
     * @param id 题库单选题主键
     * @return 结果
     */
    @Override
    public int deleteAbuwxQuestionChoiceById(Long id)
    {
        return abuwxQuestionChoiceMapper.deleteAbuwxQuestionChoiceById(id);
    }

    /**
     * 导入题库
     * @param userList
     * @param updateSupport
     * @return
     */
    @Override
    public String importUser(List<AbuwxQuestionChoice> userList, boolean updateSupport) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (AbuwxQuestionChoice user : userList)
        {
            try
            {
                // 验证是否存在这个题库
                AbuwxQuestionChoice u = abuwxQuestionChoiceMapper.selectAbuwxQuestionChoiceList(user.getIssue());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setIssue(SecurityUtils.encryptPassword(password));
//                    user.setCreateBy(operName);
                    this.insertAbuwxQuestionChoice(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getIssue() + " 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
//                    user.setUpdateBy(operName);
                    this.updateAbuwxQuestionChoice(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getIssue() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getIssue() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getIssue() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
